<?php session_start(); ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!--
//  Copyright (C) 2004-2006  Autodesk, Inc.
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of version 2.1 of the GNU Lesser
//  General Public License as published by the Free Software Foundation.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
-->

<?php include ("../config.php");
	  include ("../utils.php");
	  include(ROOT_DIR_CLASES.'/cCliente.php');?>
<script>

var cargador = parent.parent.frames["taskBar"];
cargador.document.getElementById("busyImg").style.visibility="hidden";
function formSubmitPosiActual()
{
	//document.body.style.cursor = 'wait';
	if (verificarChecks())
	{
		var cargador = parent.parent.frames["taskBar"];
		cargador.document.getElementById("busyImg").style.visibility="visible";
		document.frmHistoricoAvl.action="task_panePosiActual.php";
		document.frmHistoricoAvl.submit();
	}
	else
	{
		alert('Debe seleccionar al menos un vehiculo.');
	}
}

function formSubmitGMap() {
	var fields = document.getElementsByName('box[]');
	URL = "../gmap.php?v=";
	contadorCheckeds = 0;
	for (i = 0; i < fields.length; i++) {
		if (fields[i].checked) {
			URL = URL + fields[i].value + ",";
			contadorCheckeds ++;
		}
	}
	if (contadorCheckeds == 0) {
		alert('Debe seleccionar al menos un movil para mapear');
		return;
	}
	URL = URL.substring(0, URL.length - 1);
	var select = document.frmHistoricoAvl.cuentaSelectSegui;
	URL = URL + "&cuentaSelectSegui=" + select.value;

	var x = parent.parent.mapFrame.GetCenter().X;
    var y = parent.parent.mapFrame.GetCenter().Y; 
    URL = URL + "&x=" + x + "&y=" + y;
    
    URL = URL + "&cuenta=<?=$_SESSION['v']?>";
    
	scrollbars = 'no';
	resize = 'no';
	ancho=1070;
	alto=590;
	eval("pageGMap = window.open(URL, 'pageGMap', 'width=" + ancho + ",height=" + alto + ",menubar=NO,Status=NO,titlebar=NO,toolbar=NO,top=100,left=200,scrollbars=" + scrollbars + ",resizable=" + resize + "');");
	
}

function checkAll()
{
	var fields = document.getElementsByName('box[]');
	var select = document.frmHistoricoAvl.cuentaSelectSegui;
	var cuenta = document.frmHistoricoAvl.cuentaSelectSegui.value;
	vaciarSelector(select, fields);
	for (i = 0; i < fields.length; i++)
	{
	    if (!fields[i].checked)
	    {
			fields[i].checked = true;
	    }
	    texto = getTextoFromCheckBox(fields[i]);
		variable = new Option(texto, fields[i].value);
		select.options[select.options.length] = variable;
		if (cuenta == fields[i].value) {
			select.options[i + 1].selected = true;
		}
	    
	}
	if (document.frmHistoricoAvl.cuentaSelectSegui.value == -1 && select.options.length > 1)
    {
       	select.options[1].selected = true;
    }
}

function uncheckAll()
{
	fields = document.getElementsByName('box[]');
	for (i = 0; i < fields.length; i++)
	{  
		fields[i].checked = false ;
	}
	select = document.frmHistoricoAvl.cuentaSelectSegui;
	for (i = 0; i <= fields.length; i++)
	{
		select.options[1] = null;
	}
		
}

function vaciarSelector(select, fields) {
	for (i = 0; i <= fields.length; i++)
	{
		select.options[1] = null;
	}
}
function verificarChecks()
{
	fields = document.getElementsByName('box[]');
	for (i = 0; i < fields.length; i++)
	{
		if (fields[i].checked) return true;
	}
	return false;
}

function getTextoFromCheckBox(check) {
	var texto = check.parentNode.parentNode.innerHTML;
		pos = texto.indexOf('texto">');
		if (pos == -1)
		{
			pos = texto.indexOf('texto>');
		}
		texto = texto.substring(pos + 1);
		texto = texto.substring(texto.indexOf('>') + 1, texto.indexOf('<'));
	return texto;	
}

function clickCheckBox(check)
{
	var cuenta = document.frmHistoricoAvl.cuentaSelectSegui.value;
	var select = document.frmHistoricoAvl.cuentaSelectSegui;
	var indiceActual = select.selectedIndex; 
	var fields = document.getElementsByName('box[]');
	if (check.checked) // tengo que agregarlo al select
	{
        var selectedIndex = 0;
		vaciarSelector(select, fields);
		for (i = 0; i < fields.length; i++)
		{
			if (fields[i].checked) { 
				texto = getTextoFromCheckBox(fields[i]);
				variable = new Option(texto, fields[i].value);
				select.options[select.options.length] = variable;
				if (cuenta == fields[i].value) {
					selectedIndex = i + 1;
				}
			}
		}
		select.options[selectedIndex].selected = true;
	}
	else // lo tengo que borrar del select
	{
		var indiceSelect = 0;
		for (i = 0; i < select.options.length ; i++)
		{	
			var selectActual = select.options[i];
			if (check.value == selectActual.value)
			{
				indiceSelect = i ;
				break;
			}
		}
		select.options[indiceSelect] = null;
		if (indiceActual == indiceSelect)
		{
			if (select.options.length == 1) { 
				select.options[0].selected = true;
			} else {
				select.options[1].selected = true;
			}
		} 
	}
	
	
}
	

</script>	  
<link href="<?php echo ESTILO?>/globalStyles.css" rel="stylesheet" type="text/css">
<link href="<?php echo ESTILO?>/otherStyles.css" rel="stylesheet" type="text/css">
<link href="<?php echo ESTILO?>/viewer.css" rel="stylesheet" type="text/css">
<link href="<?php echo ESTILO?>/bykom.css" rel="stylesheet" type="text/css">

</head>

<body>
<?php 
 
  $cuentaDefault = $_SESSION['v'];
  $box = $_SESSION['cuentasSeleccionadas']; // cuentas para mapear
  $cuentaSelectSegui = $_SESSION['cuentaSelectSegui'];
  if (!isset($cuentaSelectSegui))
  {
  	$cuentaSelectSegui = $cuentaDefault;
  }
?>
<html>
	<head>
		<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
		<meta http-equiv="content-script-type" content="text/javascript">
		<meta http-equiv="content-style-type" content="text/css">
		<link href="../globalStyles.css" rel="stylesheet" type="text/css">
		<link href="../otherStyles.css" rel="stylesheet" type="text/css">
		<link href="/mapguide/viewerfiles/viewer.css" rel="stylesheet" type="text/css">
	</head>
	<body class="AppFrame">
		<form name="frmHistoricoAvl">
			<?php
			
			    if (isset($_SESSION['cliente']))
    			{
				$sessionId = ($_SERVER['REQUEST_METHOD'] == "POST")? $_POST['SESSION']: $_GET['SESSION'];
				$objetivo = $_SESSION['objetivo'];
				
				
				$cuenta = $_SESSION['v'];
				if ($objetivo == 3) {
					$cuentas = BykomCliente::Cuentas_Cliente_Movil($cuenta);
				} else {
					$cuentas = BykomCliente::Cuentas_Cliente_Movil_BIS($cuenta);
				}
				
				unset($_SESSION['posicionesActuales']);
				unset($_SESSION['posicionesAnteriores']);
				
			?>
			<input type="hidden" name="SESSION" value="<?= $sessionId ?>">

				<!-- LINK DE POSICION ACTUAL -->
				
					<span class="operation">Posici&oacute;n actual</span>
					<br>Ver ubicaci&oacute;n de los veh&iacute;culos seleccionados.
					<br>
					<br>
					<table border='0' cellspacing='0' style='border-collapse: collapse' width='100%' >
					<tr>
						<td colspan="2">
						   <input type="button" onclick="checkAll()" value="Todos">
						   <input type="button" onclick="uncheckAll()" value="Ninguno">
						</td>
					</tr>
					<tr>
						<td colspan="2">&nbsp;
					</td>
					</tr>
					</table>
					
					<div style="OVERFLOW: auto; WIDTH: 250px; TOP: 48px; HEIGHT: 200px">
					<table>
					<?php
                		   	for($i=0;$i<$cuentas->cantidad();$i++)
							{
                                $checked = "";
								if (in_array($cuentas->campo('ORDER_ID', $i), $box) || $cuentas->campo('ORDER_ID', $i) == $cuentaSelectSegui)
								{
									$checked = "checked";
								}
								echo "<tr>
                                   <td>
                                      <input type=checkbox name=box[] class='formchico' value='".$cuentas->campo('ORDER_ID',$i)."'".$checked." onClick='clickCheckBox(this);'></td>
                                   </td>
                                   <td>
                                   <span class='texto'>".
                                      $cuentas->campo('patente',$i)." - ".$cuentas->campo('identificador',$i)."
                                   </span>
                                   </td>
                                   </tr>";   
                                   
                	}?>
                	</table>
                	</div>
                	
                	<table>
                	<tr><td colspan="2">&nbsp;</td></tr>
                	
                	<tr>
                	<td colspan="2">
                	   <span class='texto'>Seguimiento:</span>
                	</td>
                	</tr>
                	<tr>
                	<td colspan="2">   
                	   <select name='cuentaSelectSegui' id="cuentaSelectSegui" class='formchico'>
                		   <option value="-1">Ninguno</option>;
                	       <?php
                		   	for($i=0;$i<$cuentas->cantidad();$i++)
							{
							 	$selected = "";
							 	if (in_array($cuentas->campo('ORDER_ID', $i), $box) || $cuentas->campo('ORDER_ID', $i) == $cuentaSelectSegui)
							 	{
									if ($cuentas->campo('ORDER_ID', $i) == $cuentaSelectSegui)
									{
										$selected = "selected";
									}
									echo "<option value='".$cuentas->campo('ORDER_ID',$i)."' ".$selected.">".
                                      	$cuentas->campo('patente',$i)." - ".$cuentas->campo('identificador',$i);"
                                   	  </option>";
								}	
                             	
                			}?>
                      	</select>
                    </td>
                    </tr>
                    
                    <tr><td colspan="2">&nbsp;</td></tr>
                	</table>
                	
                	<table width="100%">
                	<tr>
                    
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                	<td>
                		<input type="button" onclick="formSubmitPosiActual();" value="Mapeo" align="center">
                	</td>
                	<td>
                		<input type="button" onclick="formSubmitGMap();" value="Google Maps" align="center">
                	</td>
                	</tr>	
                	</table>
				</form>

	</body>
	
    <?} else 
		{
  			echo "<font class=Errors> Debe iniciar session para poder visualizar esta pagina.</font>";
		}
	?>
</html>
